/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.rest.client;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.TO.EmployeeTO;
import cz.muni.fi.pa165.carpark.rest.util.PropertyHelper;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author Martin
 */
@Component
@UrlBinding("/employee/{$event}/")
public class EmployeeClientActionBean implements ActionBean {

    private final static Logger logger = LoggerFactory.getLogger(EmployeeClientActionBean.class);
    private ActionBeanContext context;
    @SpringBean
    private RestTemplate restTemplate;
    @SpringBean
    private PropertyHelper ph;
    @ValidateNestedProperties(value = {
        @Validate(on = {"create"}, field = "name", required = true, minlength = 1)
    })
    private EmployeeTO employee;
    private boolean wasError = false;
    private String errorName = "";
    @Validate(required = true, on = "getByPosition")
    private EmployeePosition employeePosition;

    public String getErrorName() {
        return errorName;
    }

    public boolean getWasError() {
        if (wasError) {
            wasError = false;
            return true;
        }
        return false;
    }

    public EmployeeTO getEmployee() {
        return employee;
    }

    public void setEmployee(EmployeeTO employee) {
        this.employee = employee;
    }

    @DefaultHandler
    public Resolution all() {
        logger.info("rest all()");
        return new ForwardResolution("/WEB-INF/jsp/employee.jsp");
    }

    public Resolution add() {
        logger.debug("rest add employee");
        return new ForwardResolution("/WEB-INF/jsp/employeeCreate.jsp");
    }

    public Resolution create() {
        logger.debug("rest create employee={}", employee);
        restTemplate.postForObject(getURL("/"), employee, EmployeeTO.class);
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution delete() {
        logger.debug("delete({})", employee.getId());
        String id = context.getRequest().getParameter("employee.id");
        if (id != null) {
            Boolean result = restTemplate.getForObject(getURL("/delete/{id}"), Boolean.class, Long.parseLong(id));
            if (!result) {
                wasError = true;
                errorName = employee.getName();
            }
        }
        return new ForwardResolution(this.getClass(), "all");
    }

    public Resolution storno() {
        logger.debug("rest employee storno create");
        return new RedirectResolution(this.getClass(), "all");
    }

    public Resolution edit() {
        logger.debug("rest edit() employee={}", employee);
        return new ForwardResolution("/WEB-INF/jsp/employeeEdit.jsp");
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"edit", "save"})
    public void loadEmployee() {
        String id = context.getRequest().getParameter("employee.id");
        if (id == null) {
            return;
        }
        employee = restTemplate.getForObject(getURL("/{id}"), EmployeeTO.class, Long.parseLong(id));
    }

    public Resolution save() {
        logger.debug("rest save() employee={}", employee);
        restTemplate.put(getURL("/{id}"), employee, employee.getId());
        return new RedirectResolution(this.getClass(), "all");
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public EmployeeTO[] getEmployees() {
        logger.info("rest getEmployees()");
       if (this.employeePosition == null) {
            return restTemplate.getForObject(getURL("/findall"), EmployeeTO[].class);
        } else {
            return restTemplate.postForObject(getURL("/findbycategory"), this.employeePosition, EmployeeTO[].class);
        }
    }

    public EmployeePosition getEmployeePosition() {
        return employeePosition;
    }

    public void setEmployeePosition(EmployeePosition employeePosition) {
        this.employeePosition = employeePosition;
    }

    
    
    private String getURL(String request) {
        return ph.getEmployeeRestServiceURL() + request + ph.getToken();
    }
}
